package com.cloudcc.mobile.manager;

import android.content.Context;
import android.content.SharedPreferences.Editor;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.cloudcc.cloudframe.model.push.PushBindModel;
import com.cloudcc.mobile.AppConfig;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.util.MetaUtils;
import com.cloudcc.mobile.util.PreferenceUtils;

/** 
* @Description: 消息推送管理器 
* @author wangzhiqiang
* @date 2015-11-9 上午9:54:54 
*  
*/
public class CloudCCPushManager {

	private static CloudCCPushManager instance;
	
	private Context context;
	
	private CloudCCPushManager(Context context){
		this.context =context;
	}
	
	public synchronized static CloudCCPushManager getInstance(){
		if (instance==null) {
			instance =new CloudCCPushManager(AppContext.getInstance());
		}
		return instance;
	}
	
	/**
	 * 启动推送任务
	 */
	public void startPushWork(){
		String api_key = MetaUtils.getMetaValue(context, "api_key");
        PushManager.startWork(context,
                PushConstants.LOGIN_TYPE_API_KEY,api_key);
	}
	/**
	 * 恢复消息推送
	 */
	public void resumePushWork(){
		PushManager.resumeWork(context);
	}
	
	/**
	 * 停止消息推送
	 */
	public void stopPushWork(){
		PushManager.stopWork(context);
	}
	
	/** 
	* 设置地图的绑定数据
	* @param binding
	*/ 
	public void setPushBind(PushBindModel binding) {
		if (binding == null) {
			return;
		}
		Editor editor = PreferenceUtils.getEditor(context, AppConfig.XML_PUSH);
		editor.putString(IKey.PUSH_USERID, binding.userId)
				.putString(IKey.PUSH_CHANNELD, binding.channelId)
				.putString(IKey.PUSH_APPID, binding.appid)
				.putString(IKey.PUSH_REQUESTID, binding.requestId).commit();
	}

	/** 
	* 获得 消息推送的绑定
	* @return
	*/ 
	public PushBindModel getPushBind() {
		PushBindModel pushBindModel = new PushBindModel();
		pushBindModel.userId = PreferenceUtils.getString(context,  AppConfig.XML_PUSH,
				IKey.PUSH_USERID, "");
		pushBindModel.channelId = PreferenceUtils.getString(context,  AppConfig.XML_PUSH,
				IKey.PUSH_CHANNELD, "");
		pushBindModel.appid = PreferenceUtils.getString(context,  AppConfig.XML_PUSH,
				IKey.PUSH_APPID, "");
		pushBindModel.requestId = PreferenceUtils.getString(context,  AppConfig.XML_PUSH,
				IKey.PUSH_REQUESTID, "");
		return pushBindModel;
	}
	public interface IKey {

		/**
		 * 保存的类型
		 */
		String DYNAMIC_TYPE = "dynamic.type";

		/**
		 * 推送数据
		 */
		String PUSH = "push";
		/**
		 * push channel_id
		 */
		String PUSH_CHANNELD = "push.channelId";
		/**
		 * push user_id
		 */
		String PUSH_USERID = "push.userId";
		/**
		 * push request_id
		 */
		String PUSH_REQUESTID = "push.requestId";
		/**
		 * push appid
		 */
		String PUSH_APPID = "push.appid";
		
		/**
		 * 快速回复微贴的 类型
		 */
		String CHATTER_EVENTTYPES="chatter.event.type";

	}
}
